﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ComponentEditor.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.SmartInspector.Editor
{
    using UnityEditor;

    using UnityEngine;

    internal class ComponentEditor
    {
        /// <summary>
        /// El componente que edita
        /// </summary>
        private readonly MonoBehaviour target;

        /// <summary>
        /// The tabs contents
        /// </summary>
        private static GUIContent[] tabsContents;

        /// <summary>
        /// the serialized object for the target
        /// </summary>
        private Serializer.SerializedObject serializedObject;

        /// <summary>
        /// tab de los campos
        /// </summary>
        private FieldsTab fieldTab;

        /// <summary>
        /// tab de los metodos
        /// </summary>
        private MethodsTab methodTab;

        /// <summary>
        /// </summary>
        private int currentTool;

        /// <summary>
        /// Indica si se esta o no mostrando los datos del contenido
        /// </summary>
        private bool foldOut = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentEditor"/> class.
        /// </summary>
        /// <param name="component">
        /// The component.
        /// </param>
        public ComponentEditor(MonoBehaviour component)
        {
            this.target = component;
        }

        /// <summary>
        /// Gets Title.
        /// Es el t´tiulo del componente
        /// </summary>
        public string Title
        {
            get
            {
                return this.target.name;
            }
        }

        /// <summary>
        /// Gets ToolContents.
        /// </summary>
        private static GUIContent[] TabsContents
        {
            get
            {
                return tabsContents ??
                       (tabsContents =
                        new[]
                            {
                                new GUIContent("Fields"), new GUIContent("Methods"), 
                            });
            }
        }

        public void OnGUI()
        {
            if (this.serializedObject == null)
            {
                this.serializedObject = new Serializer.SerializedObject(this.target);
                this.fieldTab = new FieldsTab(this.serializedObject.FieldList, this.target);
                this.methodTab = new MethodsTab(this.serializedObject.MethodList, this.target);
            }

            this.foldOut = EditorGUILayout.InspectorTitlebar(this.foldOut, this.target);

            if (this.foldOut)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(15);
                EditorGUILayout.BeginVertical();

                this.currentTool = GUILayout.Toolbar(this.currentTool, TabsContents);

                switch (this.currentTool)
                {
                    case 0:
                        this.fieldTab.Draw();
                        break;

                    case 1:
                        this.methodTab.Draw();
                        break;
                }

                EditorGUILayout.EndVertical();
                EditorGUILayout.EndHorizontal();
            }
        }
    }
}
